//
//  WeatherModel.m
//  WeatherOFChangsha
//
//  Created by zkingsoft on 15/12/15.
//  Copyright (c) 2015年 zkingsoft. All rights reserved.
//

#import "WeatherModel.h"

@implementation WeatherModel

- (instancetype)initWithDictionary:(NSDictionary *)dic{
    self = [super init];
    if (self) {
        self.date = [dic objectForKey:@"date"];
        self.error = [dic objectForKey:@"error"];
        self.results = [dic objectForKey:@"results"];
        self.status = [dic objectForKey:@"status"];
        
        
        self.currentCity = [[[dic objectForKey:@"results"] firstObject] objectForKey:@"currentCity"];
        self.index = [[[dic objectForKey:@"results"] firstObject] objectForKey:@"index"];
        self.pm25 = [[[dic objectForKey:@"results"] firstObject] objectForKey:@"pm25"];
        self.weather_data = [[[dic objectForKey:@"results"] firstObject] objectForKey:@"weather_data"];
    }
    
    return self;
}

- (void)RequestWeatherDataWithCityName:(NSString *)cityName{
    // 百度ak 申请地址 http://lbsyun.baidu.com/apiconsole/key
    NSString *urlString = [NSString stringWithFormat:@"http://api.map.baidu.com/telematics/v3/weather?location=%@&output=json&ak=HxHQ15r7QgxLfLGUi80tVSWs&mcode=com.zkingsoft.WeatherOfChangesha",cityName];
    // 因为url中含有汉字所有要转成utf8码才行
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [DataService requestURL:urlString httpMethod:@"GET" params:nil completion:^(id result) {
        NSString *requestStatus = result[@"status"];
        if ([requestStatus isEqualToString:@"success"]) {// 请求成功
            // 如果请求成功发出一个通知 告诉已经请求到了数据
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REQUESTSUCCESSFUL" object:self userInfo:@{@"result":result}];
        }else{
            // 请求失败 也发出一个通知 告诉没有请求到数据
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REQUESTDEFAULT" object:self userInfo:nil];
        }
    }];
}

@end






